package com.taotao.job.core.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.*;

/**
 * 运行状态:0-等待运行,100-运行中,200-运行成功,404-实例丢失,500-运行失败,504-运行超时,904-杀死中,999-被杀死
 *
 * @author fanbeibei
 * @version 1.0
 * @date 2021/2/3 16:50
 */
@Getter
@AllArgsConstructor
public enum RunStatus {
    /**
     *
     */
    TRIGGER_NOT_SUCCESS(-1,"还未触发成功"),
    /**
     *
     */
    WAIT_FOR_RUN(0,"等待运行"),
    /**
     *
     */
    RUNNING(100,"运行中"),
    /**
     *
     */
    SUCCESS(200,"运行成功"),

    /**
     *
      */
    WAIT_FOR_STATUS(205,"等待异步结果上报"),

    /**
     *实例丢失-断网或执行器进程杀死导致任务运行实例状态未上报
     */
    LOST(404,"实例丢失"),
    /**
     *
     */
    FAIL(500,"运行失败"),
    /**
     *
     */
    TIMEOUT(504,"运行超时"),
    /**
     *
     */
    KILLING(998,"杀死中"),
    /**
     *
     */
    KILLED(999,"被杀死"),

    ;
    private Integer code;

    private String text;


    private static final Set<Integer> LAST_CODES = new HashSet<>(Arrays.asList(SUCCESS.code,FAIL.code,KILLED.code));


    private static final Map<Integer, RunStatus> codeTextMap = new HashMap<>();

    static {
        RunStatus[] values = RunStatus.values();
        for (RunStatus val : values) {
            codeTextMap.put(val.code, val);
        }
    }


    public static RunStatus getByCode(Integer code) {
        return codeTextMap.get(code);
    }


    /**
     * 判断是否任务最终状态,最终状态的任务不可额变更runStatus
     *
     * @return
     */
    public static boolean isLastRunStatus(Integer code) {
        return LAST_CODES.contains(code);

    }


    public static String getTextByCode(Integer code) {
        RunStatus v = getByCode(code);

        if (null == v) {
            return "未知Code:" + code;
        }
        return v.text;
    }
}
